package 剑指offer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Offer50_JZ34 {
    public static int FirstNotRepeatingChar(String str) {
        List<Character> list = new ArrayList<>();
        Map<Character, Integer> map = new HashMap<>();
        for(int i=0; i<str.length(); i++) {
            if (!map.containsKey(str.charAt(i))) {
                list.add(str.charAt(i));
                map.put(str.charAt(i), 1);
            } else {
                map.put(str.charAt(i), map.get(str.charAt(i))+1);
            }
        }
        for(Character ch : list) {
            if (map.get(ch) == 1) {
                return str.indexOf(ch);
            }
        }
        return 0;
    }

    public static char FirstNotRepeatingChar2(String str) {
        List<Character> list = new ArrayList<>();
        Map<Character, Integer> map = new HashMap<>();
        for(int i=0; i<str.length(); i++) {
            if (!map.containsKey(str.charAt(i))) {
                list.add(str.charAt(i));
                map.put(str.charAt(i), 1);
            } else {
                map.put(str.charAt(i), map.get(str.charAt(i))+1);
            }
        }
        for(Character ch : list) {
            if (map.get(ch) == 1) {
                return ch;
            }
        }
        return 'a';
    }

    public static char FirstNotRepeatingChar3(String str) {
        Map<Character, Boolean> map = new HashMap<>();
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            map.put(ch, !map.containsKey(ch));
        }
        for (char ch : chars) {
            if (map.get(ch)) {
                return ch;
            }
        }
        return ' ';
    }

    public static void main(String[] args) {
        System.out.println(FirstNotRepeatingChar3("abaccdeff"));
    }
}
